home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-02 / tpwhuge.zip / HUGEMEM.PAS < prev   
Pascal/Delphi Source File  |  1991-08-05  |  6KB  |  233 lines

  1. {$A+,B-,I-,N-,R-,S-,V-,W+,X+}
  2. Unit HugeMem;
  3. { HugeMem - manage huge global memory blocks
  4.   written by Peter Sawatzki <IN307@DHAFEU11>
  5.   (c) 1-May-1991 ver.0.1
  6.  
  7.   This unit uses two undocumented windows 'functions':
  8.     __AHShift
  9.     __AHIncr
  10.   both are used by Microsoft C and Borland C to handle the HUGE
  11.   memory model, so i think it's ok to use it
  12. }
  13. Interface
  14. Uses
  15.   WinTypes,
  16.   WinProcs;
  17.  
  18.   Procedure hRead (Var aFile: File; aHandle: THandle; Size: LongInt);
  19.   Procedure hWrite (Var aFile: File; aHandle: THandle; Size: LongInt);
  20.   Procedure hMove (srcHandle, dstHandle: THandle; Size: LongInt);
  21.   Procedure hFillChar (aHandle: THandle;  Size: LongInt; aByte: Byte);
  22.   Procedure hPutByte (aHandle: THandle; aByte: Byte; aLoc: LongInt);
  23.   Function hByteAt (aHandle: THandle; aLoc: LongInt): Byte;
  24.  
  25.   Procedure pMove (srcPtr, dstPtr: Pointer; Size: LongInt);
  26.  
  27.   Function IncPtr (aPtr: Pointer; anOffset: Word): Pointer;
  28.  
  29.   Procedure AHIncr;
  30.   Procedure AHShift;
  31.  
  32.  
  33. {NOTE: all procedures operate on unlocked memory blocks. Easily one can
  34.  add procedures to operate on locked memory blocks e.g. on Pointers, but
  35.  one must be careful not to cross segment boundaries. For example a
  36.  Move (x^,y^,$8000) will fail, if Word(x)>=$8001 !!!!!
  37.  
  38. Implementation
  39.  
  40. Procedure AHIncr;  External 'KERNEL' Index 114; {magic function}
  41. Procedure AHShift; External 'KERNEL' Index 113; {dito}
  42.  
  43. {note: AHincr is 8 in Standard and Enhanced mode, $1000 in real mode.
  44.        AHshift is 3 in Standard and Enhanced mode, 12 in real mode
  45.        (2^AHshift=AHincr)
  46. }
  47.  
  48. Const
  49.   MaxBlock = $10000 Div 2; {- n Blocks *must* fit in a 64k Segment}
  50.  
  51.  
  52. {-hrw: read/write huge amount of data:
  53.     aFile   - File to read from/write to
  54.     aHandle - Handle to memory block of at least Size bytes memory
  55.     Size    - number of bytes to transfer
  56.     rflag   - read from file if True, write to file if False
  57. }
  58. procedure hrw(Var aFile: File; aHandle: THandle; Size: LongInt; rflag: Boolean);
  59. var
  60.   Count: Word;
  61.   anAddr: Pointer;
  62. begin
  63.   anAddr:= GlobalLock(aHandle);
  64.   while Size > 0 do begin
  65.     if Size>MaxBlock Then
  66.       Count:= MaxBlock
  67.     Else
  68.       Count:= Word(Size);
  69.     If rflag Then
  70.       BlockRead(aFile, anAddr^, Count)
  71.     Else
  72.       BlockWrite(aFile,anAddr^, Count);
  73.     Dec(Size,Count);
  74.     Asm
  75.       Mov Ax,Count
  76.       Add Word Ptr anAddr,Ax
  77.       Jnc @@1
  78.       Add Word Ptr anAddr+2,OFFSET AHIncr
  79.     @@1:
  80.     End;
  81.   end;
  82.   GlobalUnlock(aHandle);
  83. end;
  84.  
  85. Procedure hread(Var aFile: File; aHandle: THandle; Size: LongInt);
  86. Begin
  87.   hrw(aFile,aHandle,Size,True)
  88. End;
  89.  
  90. Procedure hwrite(Var aFile: File; aHandle: THandle; Size: LongInt);
  91. Begin
  92.   hrw(aFile,aHandle,Size,False)
  93. End;
  94.  
  95. {-hMove: copy Size bytes from memory block srcHandle to dstHandle}
  96. Procedure hMove (srcHandle, dstHandle: THandle; Size: LongInt);
  97. Var
  98.   srcAdr, dstAdr: Pointer;
  99.   Count: Word;
  100. Begin
  101.   srcAdr:= GlobalLock(srcHandle);
  102.   dstAdr:= GlobalLock(dstHandle);
  103.   While Size>0 Do Begin
  104.     If Size>MaxBlock Then
  105.       Count:= MaxBlock
  106.     Else
  107.       Count:= Word(Size);
  108.     Move(srcAdr^,dstAdr^,Count);
  109.     Dec(Size,Count);
  110.     Asm
  111.       Mov Ax,Count
  112.       Add Word Ptr srcAdr,Ax
  113.       Jnc @@1
  114.       Add Word Ptr srcAdr+2,OFFSET AHIncr
  115.   @@1:Add Word Ptr dstAdr,Ax
  116.       Jnc @@2
  117.       Add Word Ptr dstAdr+2,OFFSET AHIncr
  118.   @@2:
  119.     End;
  120.   End;
  121.   GlobalUnlock(srcHandle);
  122.   GLobalUnlock(dstHandle);
  123. End;
  124.  
  125. {-hFillChar: fill memory block with aByte}
  126. Procedure hFillChar (aHandle: THandle;  Size: LongInt; aByte: Byte);
  127. Var
  128.   anAddr: Pointer;
  129.   Count: Word;
  130. Begin
  131.   anAddr:= GlobalLock(aHandle);
  132.   While Size>0 Do Begin
  133.     If Size>MaxBlock Then
  134.       Count:= MaxBlock
  135.     Else
  136.       Count:= Word(Size);
  137.     FillChar(anAddr^,Count,aByte);
  138.     Dec(Size,Count);
  139.     Asm
  140.       Mov Ax,Count
  141.       Add Word Ptr anAddr,Ax
  142.       Jnc @@1
  143.       Add Word Ptr anAddr+2,OFFSET AHIncr
  144.   @@1:
  145.     End;
  146.   End;
  147.   GlobalUnlock(aHandle);
  148. End;
  149.  
  150. Procedure hPutByte (aHandle: THandle; aByte: Byte; aLoc: LongInt);
  151. Var
  152.   anAddr: Pointer;
  153. Begin
  154.   anAddr:= GlobalLock(aHandle);
  155.   Asm
  156.     Mov Ax,Word Ptr aLoc
  157.     Add Word Ptr anAddr,Ax   {Mov would work as well!}
  158.     Mov Ax,Word Ptr aLoc+2
  159.     Mov Cx,OFFSET AHShift
  160.     Shl Ax,Cl                {Calculate segment}
  161.     Add Word Ptr anAddr+2,Ax
  162.   End;
  163.   Byte(anAddr^):= aByte;
  164.   GlobalUnlock(aHandle);
  165. End;
  166.  
  167. Function hByteAt (aHandle: THandle; aLoc: LongInt): Byte;
  168. Var
  169.   aPtr: Pointer;
  170. Begin
  171.   aPtr:= GlobalLock(aHandle);
  172.   Asm
  173.     Mov Ax,Word Ptr aLoc
  174.     Add Word Ptr aPtr,Ax   {Mov would work as well!}
  175.     Mov Ax,Word Ptr aLoc+2
  176.     Mov Cx,OFFSET AHShift
  177.     Shl Ax,Cl                {Calculate segment}
  178.     Add Word Ptr aPtr+2,Ax
  179.   End;
  180.   hByteAt:= Byte(aPtr^);
  181.   GlobalUnlock(aHandle);
  182. End;
  183.  
  184. Procedure pMove (srcPtr, dstPtr: Pointer; Size: LongInt);
  185. Var
  186.   Count: Word;
  187. Begin
  188.   While Size>0 Do Begin
  189.     If Size>MaxBlock Then
  190.       Count:= MaxBlock
  191.     Else
  192.       Count:= Word(Size);
  193.     Asm
  194.       Mov Ax,Word(srcPtr)
  195.       Add Ax,Count
  196.       Jnc @@1
  197.       Sub Count,Ax
  198.   @@1:Mov Ax,Word(dstPtr)
  199.       Add Ax,Count
  200.       Jnc @@2
  201.       Sub Count,Ax
  202.   @@2:
  203.     End;
  204.     Move(srcPtr^,dstPtr^,Count);
  205.     Dec(Size,Count);
  206.     Asm
  207.       Mov Ax,Count
  208.       Add Word Ptr srcPtr,Ax
  209.       Jnc @@1
  210.       Add Word Ptr srcPtr+2,OFFSET AHIncr
  211.   @@1:Add Word Ptr dstPtr,Ax
  212.       Jnc @@2
  213.       Add Word Ptr dstPtr+2,OFFSET AHIncr
  214.   @@2:
  215.     End;
  216.   End;
  217. End;
  218.  
  219.  
  220. Function IncPtr (aPtr: Pointer; anOffset: Word): Pointer;
  221. Assembler;
  222. Asm
  223.   Mov Dx,Word Ptr aPtr[2]
  224.   Mov Ax,Word Ptr aPtr
  225.   Add Ax,anOffset
  226.   Jnc @@1
  227.   Add Dx,Offset AHincr
  228. @@1:
  229. End;
  230.  
  231. End.
  232.